package com.mhc.haval.security.service.impl;

import com.mhc.haval.security.dto.DeptAreaDTO;
import com.mhc.haval.security.dto.FuncDTO;
import com.mhc.haval.security.dto.RoleDTO;
import com.mhc.haval.security.dto.UserDTO;
import com.mhc.haval.security.model.*;
import com.mhc.haval.security.service.IStaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 此类用于获取自定义用户信息
 *
 * @author Churry
 * @create 2017-08-15 15:52
 **/
@Slf4j
@Service("userDetailService")
public class SecurityUserDetailService implements UserDetailsService {
    
    @Autowired
    IStaffService iStaffService;

    //重写loadUserByUsername 方法获得 userdetails 类型用户
    @Override
    public UserDetails loadUserByUsername(String staffPhone) throws UsernameNotFoundException{
        //获取用户信息
        Optional<String> staffPhoneOptional = Optional.ofNullable(staffPhone);
        if (staffPhoneOptional.isPresent() && staffPhone != "" && staffPhone != "null") {
            UserDTO userDTO = iStaffService.getUserByPhone(staffPhone);
            Optional<UserDTO> userOptional = Optional.ofNullable(userDTO);
            if (userOptional.isPresent()) {
                List<Role> roles = new ArrayList<>();
                List<Func> funcs = new ArrayList<>();
                List<DeptArea> deptAreas = new ArrayList<>();
                //获取用户拥有的角色
                for (RoleDTO roleDTO : userDTO.getRoleList()) {
                    Role role = new Role();
                    BeanUtils.copyProperties(roleDTO, role);
                    roles.add(role);
                }
                //获取用户拥有的权限
                for (FuncDTO funcDTO : userDTO.getFuncList()) {
                    Func func = new Func();
                    BeanUtils.copyProperties(funcDTO, func);
                    funcs.add(func);
                }
                //获取部门省份
                for (DeptAreaDTO deptAreaDTO : userDTO.getDeptBizAreaList()) {
                    DeptArea deptArea = new DeptArea();
                    BeanUtils.copyProperties(deptAreaDTO, deptArea);
                    deptAreas.add(deptArea);
                }
                //授权信息，官方文档说可以是角色或者权限，我们采用权限
                Set<RestGrantedAuthority> grantedAuthorities = new HashSet<>();
                for (Func func : funcs) {
                    if (func != null && func.getFuncName() != null) {
                        grantedAuthorities.add(new RestGrantedAuthority(func.getUrl(), func.getMethod()));
                    }
                }
                User user = new User(userDTO.getPhone(), userDTO.getPassword(), grantedAuthorities);
                BeanUtils.copyProperties(userDTO, user);
                user.setRoleList(roles);
                user.setFuncList(funcs);
                user.setDeptBizAreaList(deptAreas);
                return user;
            } else {
                throw new UsernameNotFoundException("Phone number: " + staffPhone + " do not exist!");
            }
        } else {
            throw new UsernameNotFoundException("Staff phone cannot be null!");
        }
    }
}
